/* ---------------------------------------------Marlin Toolkit------------------------------------------*
Authors : Daniel Randle, Alex Bourne
Date Created : Feb 20 2013

File:
	debugwindow.cpp

Description:
	This file contains the definition for the DebugWindow class functions along with any
	helper functions associated with the class
*-------------------------------------------------------------------------------------------------------*/

// Includes
#include <global.h>
#include <logfile.h>
#include <debugwindow.h>
#include <qboxlayout.h>
#include <qtoolbar.h>
#include <qaction.h>


namespace ToolkitGui
{
/*---------------------------*
 | class DebugWindow BEGINS |
 *---------------------------*/


/* Constructor
Description:
	This ctor setups the UI for the debug widget display

Parameters:
	parent : this is the parent widget for the display - should be the main window
*/
DebugWindow::DebugWindow(QWidget * parent):
	QWidget(parent)
{
	_setupWidgets();
	_setupConnections();
}

DebugWindow::~DebugWindow()
{}

/*---------------------------------------- PRIVATE FUNCTIONS ------------------------------------------*/


/* _setupWidgets
Description:
	Set up and initialization of all widgets that are part of this class
	This includes toolbars and UI setup

Parameters:
	None

Return:
	None
*/
void DebugWindow::_setupWidgets()
{
	// init
	screen = new QTextEdit(this);
	topToolBar = new QToolBar(this);
	clrScreen = new QAction( QIcon(":/Resources/clrscreen.png"), "Clear debug screen", this);
	selectAll = new QAction( QIcon(":/Resources/selectall.png"), "Select all",this );
	copy = new QAction( QIcon(":/Resources/copy.png"), "Copy",this );

	// Setup toolbar
	topToolBar->addAction(clrScreen);
	topToolBar->addAction(selectAll);
	topToolBar->addAction(copy);
	topToolBar->setFixedHeight(30);

	// Setup page layout
	QVBoxLayout * pageLayout = new QVBoxLayout();
	pageLayout->addWidget(topToolBar);
	pageLayout->addWidget(screen);
	pageLayout->setContentsMargins(0,0,0,0);
    pageLayout->setSpacing(0);
	setLayout(pageLayout);

	// Set starting settings
	screen->setReadOnly(true);
}


/* _setupConnections
Description:
	Set up all actions/buttons connections with the appropriate slot functions so that they are functional
	when triggered

Parameters:
	None

Return:
	None
*/
void DebugWindow::_setupConnections()
{
	connect(clrScreen, SIGNAL(triggered()), this, SLOT(onClearScreen()));
	connect(selectAll, SIGNAL(triggered()), this, SLOT(onSelectAll()));
	connect(copy, SIGNAL(triggered()), this, SLOT(onCopy()));
}


/*------------------------------------------- PRIVATE SLOTS -------------------------------------------*/


/* onClearScreen
Description:
	when triggered this slot function calls the clearScreen function which clears the QTextEdit

Parameters:
	None

Return:
	None
*/
void DebugWindow::onClearScreen()
{
	clearScreen();
}


/* onSelectAll
Description:
	When triggered this slot highlights all text in the QTextEdit and switched the window focus to the 
	QTextEdit

Parameters:
	None

Return:
	None
*/
void DebugWindow::onSelectAll()
{
	screen->selectAll();
	screen->setFocus();
}


/* onCopy
Description:
	When triggered this slot copys and text highlighted in the QTextEdit to the clipboard so it can be 
	pasted anywhere needed and switches the focus to the QTextEdit

Parameters:
	None

Return:
	None
*/
void DebugWindow::onCopy()
{
	screen->copy();
	screen->setFocus();
}


/*------------------------------------------- PUBLIC INTERFACE ---------------------------------------*/


/* writeToScreen
Description:
	writes the given text to the QTextEdit - appending it as a new line

Parameters:
	text : Text that is to be appended to the QTextEdit

Return:
	None
*/
void DebugWindow::writeToScreen(const QString & text)
{
	// newline after every entry by default with append
	screen->append(text);
}


/* clearScreen
Description:
	clears the QTextEdit contents

Parameters:
	None

Return:
	None
*/
void DebugWindow::clearScreen()
{
	screen->clear();
}


/*---------------------------*
 | class DebugDisplay ENDS   |
 *---------------------------*/
}